from typing import List

"""
思路：
总结一个规律来实现
先按行来考虑，如果一行的第一个是0，那就翻转，因为后面不管多大，都不如第一个大
行全部考虑过了，就可以按列来考虑了，如果这一列的0超过一半，那翻转这一列。
"""


class Solution:
    def matrixScore(self, A: List[List[int]]) -> int:
        m = len(A)
        n = len(A[0])

        def turn_row(num):
            for i in range(n):
                if A[num][i] == 0:
                    A[num][i] = 1
                else:
                    A[num][i] = 0

        def turn_col(num):
            for i in range(m):
                if A[i][num] == 0:
                    A[i][num] = 1
                else:
                    A[i][num] = 0

        for i in range(m):
            if A[i][0] == 0:
                turn_row(i)
        for j in range(n):
            zero_count = 0
            for i in range(m):
                if A[i][j] == 0:
                    zero_count += 1

            if zero_count > m / 2:
                turn_col(j)

        res = 0
        for i in range(m):
            res += int("".join([str(a) for a in A[i]]), 2)
        return res


if __name__ == "__main__":
    A = [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
    s = Solution().matrixScore(A)
    print(s)
